package ArrayPackage;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lzm
 * @version 1.0
 */
public class removeElement_ {

  @Test
  public void removeElementTest(){
    int[] nums = new int[]{0,1,2,2,3,0,4,2};
    int val = 2;
    System.out.println(removeElement(nums,val));
    System.out.println();
  }

  public int removeElement(int[] nums, int val) {
    /*创建集合存储不是val的值
    List<Integer> list = new ArrayList<>();
    for(int i = 0 ; i < nums.length ; i++){
      if (nums[i] != val){
        list.add(nums[i]);
      }
      else{
        continue;
      }
    }
    System.out.println(list);
    return list.size();
  }*/


    /*双指针*/
    int slow = 0;
    for (int i = 0 ; i < nums.length ; i++){
      if (nums[i] != val){
        nums[slow++] = nums[i];
      }
    }
    return slow;
  }
}
